package com.cy.pj.module.controller;

import com.cy.pj.module.pojo.ResponseResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class JsonController {

    @RequestMapping("/doConvertListToJson")
    @ResponseBody
    public List<Map<String,Object>> doConvertListToJson(){
        List<Map<String, Object>> list = new ArrayList<>();
        HashMap<String, Object> map = new HashMap<>();
        map.put("name","tony");
        map.put("state", true);
        list.add(map);
//        map=new HashMap<>();
        map.put("name1","Jason");
        map.put("state1", true);
//        list.add(map);
        return list;
    }

    @RequestMapping("/doConvertMapToJson")
    @ResponseBody
    public Map<String ,Object> doConvertMapToJson(){
        Map<String, Object> map = new HashMap<>();
        map.put("name", "读到");
        map.put("state", "no");
        return map;
    }

    /**
     * @ResponseBody 不将响应结果理解为view,
     * 并将响应结果转化为json格式(必须满足Json格式转化条件),
     * 写到响应体,响应到客户端
     * @return
     */
    @RequestMapping("/doConvertResponseToJson")
    @ResponseBody //不将响应结果理解为view,并将响应结果转化为json格式,写到响应体,响应到客户端
    public ResponseResult doConvertResponseToJson(){
        ResponseResult result = new ResponseResult();
        result.setCode(200);
        result.setMessage("ok");
        return result;
        //当此对象返回时,spring web模块会调用jackson api,将返回值转换为json格式字符串
        //底层转换原理分析?
    }
}
